package org.aplikator.server.descriptor;

import org.aplikator.client.shared.descriptor.BinaryFieldDTO;
import org.aplikator.client.shared.descriptor.EntityDTO;
import org.aplikator.client.shared.descriptor.PropertyDTO;
import org.aplikator.client.shared.descriptor.WidgetDTO;
import org.aplikator.server.Context;
import org.aplikator.server.data.BinaryData;

public class BinaryField extends WidgetPropertyDescriptorBase<BinaryData> {

    public BinaryField(Property<BinaryData> property) {
        super(property);
    }

    @Override
    public WidgetDTO getWidgetDescriptor(Context ctx) {
        BinaryFieldDTO desc = new BinaryFieldDTO(untypedClientClone(ctx), new EntityDTO( getProperty().getEntity().getId(), getProperty().getEntity().getLocalizedName(ctx)));
        desc.setSize(getSize());
        desc.setEnabled(isEnabled());
        return desc;
    }

    private PropertyDTO untypedClientClone(Context ctx){
        Property<BinaryData> property = getProperty();
        PropertyDTO clientRefThrough = null;
        if (property.getRefferedThrough() != null) {
            clientRefThrough = property.getRefferedThrough().clientClone(ctx);
        }
        PropertyDTO retval = new PropertyDTO(property.getId(), property.getLocalizedName(ctx), property.getType().getName(), property.getSize(), clientRefThrough, property.isEditable());
        return retval;
    }

    @Override
    public void registerProperties(Form form) {
        //form.addProperty(property);
    }
}
